﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Tools.Framework;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Common.Framework
{
    public class OrderModelField
    {
        public string OrderField { set; get; }
        public bool IsDesc { set; get; }
    }


    /// <summary>
    ///     分页信息类
    /// </summary>
    /// <typeparam name="TEntity">TEntity</typeparam>
    [Serializable, XmlRoot("Root")]
    public class PageData<TEntity>
    {

        public List<OrderModelField> OrderFiledList { set; get; }

        private Hashtable _hash;
        private string[] _include;
        private int _pageIndex = 1;
        private int _pageSize = 20;

        public PageData()
        {
            //this.Where = a => true;
            this.EntSQLParameters = new List<ObjectParameter>();
        }

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="form">http 请求集合对象 FormCollection</param>
        public PageData(NameValueCollection form)
            : this()
        {
            //每页显示大小
            if (!string.IsNullOrEmpty(form["pageSize"]))
                this.PageSize = Convert.ToInt32(form["pageSize"]);

            //页码
            if (!string.IsNullOrEmpty(form["pageIndex"]))
                this.PageIndex = Convert.ToInt32(form["pageIndex"]) + 1;

            //排序字段
            if (!string.IsNullOrEmpty(form["sortField"]))
                this.OrderField = form["sortField"];

            //排序类型
            if (!string.IsNullOrEmpty(form["sortOrder"]))
                this.IsDesc = !form["sortOrder"].Equals("asc", StringComparison.OrdinalIgnoreCase);
        }


        /// <summary>
        ///     条件表达式树
        /// </summary>
        [XmlIgnore]
        public Expression<Func<TEntity, bool>> Where { get; set; }

        /// <summary>
        /// entity sql
        /// </summary>
        public string EntSQL { get; set; }

        /// <summary>
        /// entsql 参数
        /// </summary>
        public List<ObjectParameter> EntSQLParameters { get; set; }

        /// <summary>
        ///     排序字段
        /// </summary>
        [XmlIgnore]
        public string OrderField { get; set; }

        /// <summary>
        ///     排序类型
        /// </summary>
        public bool IsDesc { get; set; }

        /// <summary>
        ///     要抓取的导航属性
        /// </summary>
        [XmlIgnore]
        public string[] Include
        {
            get { return this._include; }
            set { this._include = value; }
        }


        /// <summary>
        ///     页大小
        /// </summary>
        public int PageSize
        {
            get { return this._pageSize; }
            set { this._pageSize = value; }
        }

        /// <summary>
        ///     页号
        /// </summary>
        public int PageIndex
        {
            get { return this._pageIndex; }
            set { this._pageIndex = value; }
        }

        /// <summary>
        ///     总条数
        /// </summary>
        public long TotalCount { get; set; }

        /// <summary>
        ///     总页数
        /// </summary>
        public long PageCount { get; set; }

        /// <summary>
        ///     分页结果
        /// </summary>
        public List<TEntity> Data { get; set; }


        /// <summary>
        ///     DTO装载分页结果
        /// </summary>
        public dynamic DTOData { get; set; }


        /// <summary>
        ///     mini ui  datagrid 分页数据 TEntity
        /// </summary>
        public Hashtable Hash
        {
            get
            {
                if (this._hash == null)
                    this._hash = new Hashtable();
                this._hash["data"] = this.Data;
                this._hash["total"] = this.TotalCount;
                return this._hash;
            }
        }


        /// <summary>
        ///     mini ui  datagrid 分页数据 TDEntity
        /// </summary>
        public Hashtable HashDTO
        {
            get
            {
                if (this._hash == null)
                    this._hash = new Hashtable();
                this._hash["data"] = this.DTOData;
                this._hash["total"] = this.TotalCount;
                return this._hash;
            }
        }


        /// <summary>
        ///     将数据结果装载到 DTO 结果中
        /// </summary>
        /// <typeparam name="TDEntity"></typeparam>
        public void LoadDTO<TDEntity>() where TDEntity : class
        {
            if (this.Data != null)
            {
                this.DTOData = ObjectMapper.Mapper<List<TEntity>, List<TDEntity>>(this.Data);
            }
        }


        /// <summary>
        ///     对象转成 ArrayList
        /// </summary>
        /// <returns></returns>
        public ArrayList ToArrayList()
        {
            var array = new ArrayList();
            if (this.Data != null)
            {
                PropertyInfo[] ps = typeof(TEntity).GetProperties(BindingFlags.Public |
                                                                   BindingFlags.Instance);
                foreach (TEntity item in this.Data)
                {
                    var hash = new Hashtable();
                    foreach (PropertyInfo propertyInfo in ps)
                    {
                        object value = propertyInfo.GetValue(item, null);
                        hash[propertyInfo.Name] = value ?? string.Empty;
                    }
                    array.Add(hash);
                }
            }
            return array;
        }
    }
}
